/*
 * This file is part of Euphrat.
 *
 * Copyright © 2012 Bart Verhoeven
 *
 * Euphrat is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Euphrat is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Euphrat. If not, see <http://www.gnu.org/licenses/>.
 */
package org.euphrat.client.view.widget;

import org.euphrat.client.controller.login.LoginController;
import org.euphrat.client.controller.login.LoginListener;
import org.euphrat.client.view.i18n.LoginPanelConstants;
import org.euphrat.client.view.image.Images;
import org.euphrat.client.service.login.LoginContext;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;

/**
 * <p>A panel that displays information about the user that is logged in.</p>
 *
 * <p>The following CSS rules are applied:
 * <ul>
 *   <li>{@code .loginPanel} (the actual login panel)</li>
 *   <li>{@code .loginPanel-Account} (the login account label)</li>
 * </ul>
 * </p>
 *
 * @author Bart Verhoeven
 */
public class LoginPanel extends Composite {

  /** Separator for URL and email address. */
  private static final Label separator;
  /** Resource bundle with constants. */
  private static final LoginPanelConstants constants;

  static {
    // Initialize static variables.
    separator = new Label("|");
    constants = GWT.create(LoginPanelConstants.class);
  }

  /** Actual login panel. */
  private final HorizontalPanel panel = new HorizontalPanel();
  /** URL that redirects the user for (de-)authentication. */
  private final Anchor authenticationURL = new Anchor();
  /** Label that holds the user's email address. */
  private final Label emailAddress = new Label();
  /** Busy loading icon. */
  private final Image loading;

  /**
   * Creates a new login panel.
   */
  public LoginPanel() {
    // Get the busy loading image.
    Images images = GWT.create(Images.class);
    loading = new Image(images.loading());

    // Apply some styling.
    panel.setStyleName("loginPanel");
    emailAddress.setStyleName("loginPanel-Account");

    // Put the login panel together.
    panel.add(emailAddress);
    panel.add(separator);
    panel.add(authenticationURL);

    // Make sure this panel responds to login attempts.
    LoginController controller = LoginController.getInstance();
    LoginPanelUpdater updater = new LoginPanelUpdater();
    controller.addLoginListener(updater);

    initWidget(panel);
  }

  /**
   * Listener that updates the login panel when login state of the user changed.
   */
  private class LoginPanelUpdater implements LoginListener {
    private HandlerRegistration handler;

    @Override
    public void onLoginFailed(LoginContext context) {
      panel.remove(loading);
      authenticationURL.setText(constants.retry());

      authenticationURL.setVisible(true);
      separator.setVisible(false);
      emailAddress.setVisible(false);

      if (handler != null) {
        handler.removeHandler();
      }
      handler = authenticationURL.addClickHandler(new RetryLoginHandler());
    }

    @Override
    public void onLoginRequested(LoginContext context) {
      emailAddress.setVisible(false);
      separator.setVisible(false);
      authenticationURL.setVisible(false);

      if (handler != null) {
        handler.removeHandler();
      }

      panel.add(loading);
    }

    @Override
    public void onLoggedIn(LoginContext context) {
      panel.remove(loading);

      emailAddress.setText(context.getEmailAddress());
      authenticationURL.setText(constants.logout());
      authenticationURL.setHref(context.getLogoutURL());

      emailAddress.setVisible(true);
      separator.setVisible(true);
      authenticationURL.setVisible(true);
    }

    @Override
    public void onLoggedOut(LoginContext context) {
      panel.remove(loading);

      authenticationURL.setText(constants.login());
      authenticationURL.setHref(context.getLoginURL());

      emailAddress.setVisible(false);
      separator.setVisible(false);
      authenticationURL.setVisible(true);
    }
  }

  /**
   * Handler that re-attempts to login after a click.
   */
  private static class RetryLoginHandler implements ClickHandler {
    @Override
    public void onClick(ClickEvent event) {
      LoginController.getInstance().login();
    }
  }
}
